const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    phone: '',
    address: '',
    password: '123456',
    userNameRules: [
      { required: true, message: '请输入客户名称', trigger: 'blur' },
      { min: 2, max: 10, message: '客户名称长度在2-10个字符之间', trigger: 'blur' },
    ],
    phoneRules:  [
      // { required: true, message: '请输入账号码', trigger: 'blur' },
      // { min: 11, max: 11, message: '账号码格式不正确', trigger: 'blur' }
    ],
    addressRules: [
      { required: true, message: '请输入地址', trigger: 'blur' },
      { min: 2, max: 50, message: '地址长度在2-50个字符之间', trigger: 'blur' },
    ],
    // passwordRules: [
    //   { required: true, message: '请输入登录密码', trigger: 'blur' },
    //   { min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur' }    ],
  },
  submit(event){
    const {detail} = event;
    console.log(99,detail.values);
    if(detail.isValidate){
      var httpurl='/addCustomer'
      if(this.data.id){
        detail.values.id=this.data.id
        httpurl='/upCustomer';
      }
      //提交detail.values
      wx.request({
        url: app.globalData.onlineUrl+httpurl,
        method: 'POST',
        data: detail.values,
        header: {'content-type': 'application/json' }, // 请求头
        success: function (res) {
          console.log(123456,);
          if(res.data.code=='-1'){
            wx.showToast({
              title: res.data.msg,
              icon:'none'
            });
          }else{
            wx.navigateTo({
              url: '/pages/navigator/customer/index'
            });
          }
          
        },
        fail: function (err) {

        }
      });    
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    wx.lin.initValidateForm(this);
    console.log(133,option.id);
    this.setData({
      id:option.id
    })
    if(this.data.id){
      let _this=this;
      //请求客户数据
      wx.request({
        url: app.globalData.onlineUrl+'/getCustomerById',
        method: 'POST',
        data: {id:option.id},
        header: {'content-type': 'application/json' }, // 请求头
        success: function (res) {
          let data=res.data.data;
          _this.setData({
            username: data.username,
            phone: data.phone,
            address: data.address
          })
        },
        fail: function (err) {
        }
      }); 
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});
